﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Research.Kinect.Nui;

namespace IntelBillboard
{
    class GestureController
    {
        //huajun
//beihong hahahah   huajun yeyeye
//bei hong 3
        const float DELTA = 0.2f;
        DateTime select_check_time = DateTime.MaxValue;
        Microsoft.Research.Kinect.Nui.Vector selectPreRP = new Microsoft.Research.Kinect.Nui.Vector();

        private bool isZeroVector(Microsoft.Research.Kinect.Nui.Vector aVector)
        {
            if (aVector.X == 0 && aVector.Y == 0 && aVector.Z == 0)
                return true;
            else
                return false;
        }

        bool isApproxEqualVector2(Microsoft.Research.Kinect.Nui.Vector vectorA, Microsoft.Research.Kinect.Nui.Vector vectorB, float delta)
        {
            if (isApproxEqual2(vectorA.X, vectorB.X, delta) && isApproxEqual2(vectorA.Y, vectorB.Y, delta) && isApproxEqual2(vectorA.Z, vectorB.Z, delta))
                return true;

            return false;
        }

        private bool isApproxEqual3(float a, float b, float c, float delta)
        {
            return Math.Abs(a - b) <= delta && Math.Abs(b - c) <= delta && Math.Abs(a - c) <= delta;
        }

        private bool isApproxEqual2(float a, float b, float delta)
        {
            return Math.Abs(a - b) <= delta;
        }

        public bool checkSelect(JointsCollection joints)
        {
            Microsoft.Research.Kinect.Nui.Vector HandRP = joints[JointID.HandRight].Position;
            //Microsoft.Research.Kinect.Nui.Vector HandLP = joints[JointID.HandLeft].Position;
            Microsoft.Research.Kinect.Nui.Vector SpineP = joints[JointID.Spine].Position;

            /*
            if (isZeroVector(selectPreLP))
            {
                selectPreLP = HandLP;
            }*/

            if (isZeroVector(selectPreRP))
            {
                selectPreRP = HandRP;
            }

            // if hand is in front of the body
            if (isApproxEqualVector2(HandRP, selectPreRP, DELTA))
            {
                if (select_check_time == DateTime.MaxValue)
                {
                    select_check_time = DateTime.Now;
                }
                else
                {
                    DateTime cur_time = DateTime.Now;
                    if (cur_time.Subtract(select_check_time) > TimeSpan.FromSeconds(3))
                        return true;
                }
            }
            else
            {
                select_check_time = DateTime.MaxValue;
                selectPreRP.X = 0;
                selectPreRP.Y = 0;
                selectPreRP.Z = 0;
            }

            return false;
        }
    }
}
